package deyi.http;

import com.sun.istack.internal.NotNull;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.net.InetSocketAddress;

@Component("httpserver")
@Data
@NoArgsConstructor
public class HttpServer {

    private com.sun.net.httpserver.HttpServer server;
    @Value(value = "${localHttpPort}")
    private int port;
    @Autowired
    private SessionFactory sessionFactory;

    @PostConstruct
    public void runServer() throws Exception{
        InetSocketAddress addr = new InetSocketAddress(port);
        server = com.sun.net.httpserver.HttpServer.create(addr,100);
        server.createContext("/",new HttpEventHandle());
        server.setExecutor(null);
        server.start();
        System.out.println("http server start ... port:" + port);
    }

    @PreDestroy
    public void stopServer(){
        server.stop(port);
    }
}
